<%-- 
    Document   : SearchResult
    Created on : Mar 8, 2012, 3:09:36 AM
    Author     : maconcon
--%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.lang.Integer" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%
            response.setHeader("Pragma","no-cache");
            response.setHeader("Cache-Control","no-store");
            response.setHeader("Expires","0");
            response.setDateHeader("Expires",-1);
        %>
        
          <%
        if(session.getAttribute("user")== null){
            response.sendRedirect("index.jsp");
            }

               else{
        %>
        
    </head>
    <body>
       <%
   String name = request.getParameter("search");
   session.setAttribute( "search", name );
%>
      <%
    try {

String connectionURL = "jdbc:mysql://localhost:3306/databasefoobar";
// declare a connection by using Connection interface
Connection connection = null;
/* declare object of Statement interface that is used for executing sql
statements. */
Statement statement = null;
// declare a resultset that uses as a table for output data from tha table.
ResultSet rs = null;
ResultSet rs2= null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters
of string type connection url, user name and password to connect to database.*/
connection = DriverManager.getConnection(connectionURL, "root", "123456");
/* createStatement() is used for create statement object that is used for
sending sql statements to the specified database. */
int x=Integer.parseInt(session.getAttribute("id").toString());
String 	add=	session.getAttribute("address").toString();
String city=		session.getAttribute("city").toString();
String post=	session.getAttribute("postal").toString();

String query1="select tempcart.productid, products.productid,products.title,products.synopsis,products.price,products.type, tempcart.customerid from tempcart,products where tempcart.customerid=";
statement = connection.createStatement();
         PreparedStatement pstmt = connection.prepareStatement("select cartitems.cartid, cartitems.productid, cartitems.quantity,products.productid,products.title,products.synopsis,products.price,products.type, cartitems.customerid from cartitems,products where cartitems.customerid=? AND cartitems.productid=products.productid group by cartitems.productid");
// PreparedStatement pstmt = connection.prepareStatement("select title,synopsis,price,type,quantity,cartid from products"+
//"inner join tempcart on tempcart.productid=products.productid");
pstmt.setInt(1, x);

 rs = pstmt.executeQuery();

 int price=0;

// sql query to retrieve values from the secified table.
%>

 <a href="BrowseProducts.jsp"> Browse Products </af>
        <a href="customerprofile.jsp"> View Profile </af>
              <a href="customer.jsp"> Home </af>
        <a href="UserLogoutServlet"> Logout </a>


  <form action="SearchResult.jsp" method="POST">
            Search Products: <input type="text" name="search" /><input type="submit" value="Search" />
        </form>
      <% out.print(" " +session.getAttribute("id")+ "");%>
      <% out.print("Welcome " +session.getAttribute("user")+ "");%>
 <TABLE BORDER="1">
            <TR>
            
                <TH>Title</TH>
                <TH>Synopsis</TH>
                <TH>Type</TH>
                <TH>Price</TH>
                <TH>AddReivew</TH>
          
            </TR>
            <% while(rs.next()){ %>
            <TR>
               
               
            
                <TD> <%= rs.getString("products.title") %></TD>
                <TD> <%= rs.getString("products.synopsis") %></TD>
                 <TD> <%= rs.getString("products.type") %> </TD>
                      <TD> <%= rs.getInt("products.price") %>   x</td>
                       
                      <td> <form action="AddReview" method="post">
                              <input type="text" name="review">
                                <input type="hidden" name="productid" value=<%= rs.getInt("products.productid") %>  />       
                         <input type="hidden" name="customerid" value=<%=x%> /> 
                         <input type="submit" value="ADD" />
                          </form>  
                          
                           </TR>
            <% } %>
        </TABLE>
        
    <font size="3">Total: <%=price%>   </font>
    <% /*String 	add=	session.getAttribute("address").toString();
String city=		session.getAttribute("city").toString();
String post=	session.getAttribute("postal").toString();*/%>
      
         
            
<%

// close all the connections.
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%> 
        
    </body>

    <%
    }
        %>
</html>
